package com.example.demo.web;

import com.example.demo.hk.ClientDemo.HCNetTools;
import com.example.demo.hk.CommonKit;
import com.example.demo.hk.VideoUtil;
import com.example.demo.hk.entity.Device;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;


@Controller
@RequestMapping("/dev")
public class DeviceController {
	/**
	 * 开始推流
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getRTMPUrlAndStartTranscode")
	public Object getRTMPUrlAndStartTranscode(String id){
		Device dev = new Device();
		dev.setAccount("admin");
		dev.setPassword("Ds@123456");
		dev.setIp("192.168.1.64");
		dev.setPort("8000");
		VideoUtil utils = new VideoUtil();
		String appName ="admin192168164";//appName为用户账号和摄像头ip去"."后的组合
		Map<String,String> map = utils.startTranscodeAndGetTasker(dev,appName,false);
		if(StringUtils.isNotEmpty(map.get("tasker"))){
			map.put("rtmpUrl","rtmp://"+CommonKit.getServerIp()+":1935/live/"+appName);
		}
		return map;
	}

	/**
	 * 回放推流
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getBackUrl")
	public Object getBackUrl(String id){
		Device dev = new Device();
		dev.setAccount("admin");
		dev.setPassword("Ds@123456");
		dev.setIp("192.168.1.64");
		dev.setPort("8000");
		VideoUtil utils = new VideoUtil();
		String appName ="aaa"+ UUID.randomUUID().toString();//appName为用户账号和摄像头ip去"."后的组合
		Map<String,String> map = utils.startTranscodeAndGetTasker(dev,appName,true);
		if(StringUtils.isNotEmpty(map.get("tasker"))){
			map.put("rtmpUrl","rtmp://"+CommonKit.getServerIp()+":1935/live/"+appName);
		}
		return map;
	}

	/* *
	 * @Description:  抓图
	 * @param
	 * @Return：java.lang.Object
	 * @Author: miaozm
	 * @Date: 2020/4/7 10:57
	 */
	@ResponseBody
	@RequestMapping("/catchPic")
	public Object catchPic(){
		Device dev = new Device();
		dev.setAccount("admin");
		dev.setPassword("Ds@123456");
		dev.setIp("192.168.1.64");
		dev.setPort("8000");
		HCNetTools hcTool = new HCNetTools();
		// 保存图片地址
		hcTool.getDVRPic(dev,1,"D:\\profile\\2020-04-07\\aaa.jpg");
		return null;
	}
	/* *
	 * @Description:  下载视频
	 * @param
	 * @Return：java.lang.Object
	 * @Author: miaozm
	 * @Date: 2020/4/7 10:57
	 */
	@ResponseBody
	@RequestMapping("/downloadVideo")
	public Object downloadVideo() throws ParseException {
		Device dev = new Device();
		dev.setAccount("admin");
		dev.setPassword("Ds@123456");
		dev.setIp("192.168.1.64");
		dev.setPort("8000");
		HCNetTools hcTool = new HCNetTools();
		SimpleDateFormat dtf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date begin = dtf.parse("2020-04-08 14:00:00");
		Date end = dtf.parse("2020-04-08 14:05:00");
		// 保存视频地址
		hcTool.downloadVideo(dev,begin,end,"D:\\profile\\2020-04-08\\aaab.mp4",1);
		return null;
	}

	/* *
	 * @Description:  跳转视频列表
	 * @param
	 * @Return：java.lang.Object
	 * @Author: miaozm
	 * @Date: 2020/4/7 10:57
	 */
	/*@RequestMapping("/getFileList")
	public Object getFileList(String startTime,String endTime,ModelMap map) throws ParseException {
		Device dev = new Device();
		dev.setAccount("admin");
		dev.setPassword("Ds@123456");
		dev.setIp("192.168.1.64");
		dev.setPort("8000");
		HCNetTools hcTool = new HCNetTools();
		SimpleDateFormat dtf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date begin = dtf.parse("2020-04-08 14:00:00");
		Date end = dtf.parse("2020-04-08 14:05:00");
		List fileList = hcTool.getFileList(dev, begin, end, 1);
		map.put("fileList",fileList);
		return "hk/backList";
	}*/

}
